package com.ruantu.clothing.service.impl;

import com.ruantu.clothing.dao.*;
import com.ruantu.clothing.entity.*;
import com.ruantu.clothing.service.ProductionService;
import com.ruantu.clothing.util.MyException;
import com.ruantu.clothing.util.UUIDUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018/5/28 0028.
 */
@Service
public class ProductionServiceImpl implements ProductionService {

    @Autowired
    private ProductionMapper productionMapper;
    @Autowired
    private GongyingshangMapper gongyingshangMapper;
    @Autowired
    private ProductionDetailsMapper productionDetailsMapper;
    @Autowired
    private RukuMapper rukuMapper;


    @Override
    public List<Production> findAll(Production production, PageBean pageBean) {
        Map<String, Object> map = new HashMap<>();
        if (production.getProduct() != null && !"".equals(production.getProduct())) {
            map.put("product", "%" + production.getProduct() + "%");
        }
        if (production.getState() != null && !"".equals(production.getState()) && -1 != production.getState()) {
            map.put("state", production.getState());
        }
        map.put("jgId", production.getJgId());
        map.put("start", pageBean.getStart());
        map.put("rows", pageBean.getRows());
        //查询总数量
        int count = productionMapper.findAllCount(map);
        pageBean.setTotal(count);
        List<Production> list = productionMapper.findAll(map);
        for (int i = 0; i < list.size(); i++) {
            List<String> colors = new ArrayList<String>();
            List<String> sizes = new ArrayList<String>();
            //颜色去重复
            for (int j = 0; j < list.get(i).getProductionDetails().size(); j++) {
                if (!colors.contains(list.get(i).getProductionDetails().get(j).getColor())) {
                    colors.add(list.get(i).getProductionDetails().get(j).getColor());
                }
                if (!sizes.contains(list.get(i).getProductionDetails().get(j).getSize())) {
                    sizes.add(list.get(i).getProductionDetails().get(j).getSize());
                }
            }
            Collections.reverse(sizes);
            list.get(i).setColors(colors.toString());
            list.get(i).setSizes(sizes.toString());
            list.get(i).setProductionDetails(null);
        }
        return list;
    }

    @Override
    public XSSFWorkbook downExcel(Production production) {
        Map<String, Object> map = new HashMap<>();
        if (production.getProduct() != null && !"".equals(production.getProduct())) {
            map.put("product", "%" + production.getProduct() + "%");
        }
        if (production.getState() != null && !"".equals(production.getState()) && -1 != production.getState()) {
            map.put("state", production.getState());
        }
        map.put("jgId", production.getJgId());
        List<Production> list = productionMapper.findAllForExcel(map);
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFont(font);
        // 设置这些样式
        int count = 0;
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("款号");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("款号名称");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("商品编号");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("颜色");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("尺码");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("铭牌价");
        cell.setCellStyle(titleStyle);
        if ("1".equals(production.getJgId())) {
            cell = row.createCell(++count);
            cell.setCellValue("供应商");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("款式");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("品牌");
            cell.setCellStyle(titleStyle);
        }
        cell = row.createCell(++count);
        cell.setCellValue("年份");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("季节");
        cell.setCellStyle(titleStyle);
        if ("1".equals(production.getJgId())) {
            cell = row.createCell(++count);
            cell.setCellValue("款型");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("安全级别");
            cell.setCellStyle(titleStyle);
        }
        cell = row.createCell(++count);
        cell.setCellValue("性别");
        cell.setCellStyle(titleStyle);
        if ("1".equals(production.getJgId())) {
            cell = row.createCell(++count);
            cell.setCellValue("大类");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("小类");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("执行标准");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("里料");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("填充物");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("撞料");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("面料");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("胆布");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(++count);
            cell.setCellValue("备注");
            cell.setCellStyle(titleStyle);
        }
        cell = row.createCell(++count);
        cell.setCellValue("创建人");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("创建时间");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("修改人");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("修改时间");
        cell.setCellStyle(titleStyle);
        row.setHeight((short) 450);
        CellStyle titleStyle2 = wb.createCellStyle();
        titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        for (int i = 0; i < list.size(); i++) {
            count = 0;
            row = sheet.createRow(i + 1);
            //款号
            cell = row.createCell(count);
            cell.setCellValue(list.get(i).getProduct());
            cell.setCellStyle(titleStyle2);
            //款号名称
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getProductName());
            cell.setCellStyle(titleStyle2);
            //商品编号
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getProductCode());
            cell.setCellStyle(titleStyle2);
            //颜色
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getColors());
            cell.setCellStyle(titleStyle2);
            //尺码
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getSizes());
            cell.setCellStyle(titleStyle2);
            //铭牌价
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getPrice());
            if ("1".equals(production.getJgId())) {
                //供应商
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getGname());
                cell.setCellStyle(titleStyle2);
                //款式
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getKuanshi());
                cell.setCellStyle(titleStyle2);
                //品牌
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getPinpai());
                cell.setCellStyle(titleStyle2);
            }
            //商品年份
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getYear());
            cell.setCellStyle(titleStyle2);
            //商品季节
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getSeason());
            cell.setCellStyle(titleStyle2);
            if ("1".equals(production.getJgId())) {
                //款型
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getStyle());
                cell.setCellStyle(titleStyle2);
                //安全级别
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getAnquanjibie());
                cell.setCellStyle(titleStyle2);
            }
            //性别
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getSex());
            cell.setCellStyle(titleStyle2);
            if ("1".equals(production.getJgId())) {
                //大类
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getDalei());
                cell.setCellStyle(titleStyle2);
                //小类
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getCategory());
                cell.setCellStyle(titleStyle2);
                //执行标准
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getZhixingbiaozhun());
                cell.setCellStyle(titleStyle2);
                //里料
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getLiliao());
                cell.setCellStyle(titleStyle2);
                //填充物
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getTianchongwu());
                cell.setCellStyle(titleStyle2);
                //撞料
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getZhuangliao());
                cell.setCellStyle(titleStyle2);
                //面料
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getMianliao());
                cell.setCellStyle(titleStyle2);
                //胆布
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getDanbu());
                cell.setCellStyle(titleStyle2);
                //备注
                cell = row.createCell(++count);
                cell.setCellValue(list.get(i).getBeizhu());
                cell.setCellStyle(titleStyle2);
            }
            //创建人
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getCreateRen());
            cell.setCellStyle(titleStyle2);
            //创建时间
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getCreateTime());
            cell.setCellStyle(titleStyle2);
            //最后更新人
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getUpdateRen());
            cell.setCellStyle(titleStyle2);
            //最后更新时间
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getUpdateTime());
            cell.setCellStyle(titleStyle2);
        }
        for (int i = 0; i <= count; i++) {
            sheet.setColumnWidth(i, 4000);
        }
        return wb;
    }


    @Override
    public XSSFWorkbook download() {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFont(font);
        // 设置这些样式
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("款号");
        cell.setCellStyle(titleStyle);

        cell = row.createCell(1);
        cell.setCellValue("款号名称");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(2);
        cell.setCellValue("颜色");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(3);
        cell.setCellValue("尺码");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(4);
        cell.setCellValue("铭牌价");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(5);
        cell.setCellValue("款式");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(6);
        cell.setCellValue("品牌");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(7);
        cell.setCellValue("年份");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(8);
        cell.setCellValue("季节");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(9);
        cell.setCellValue("款型");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(10);
        cell.setCellValue("安全级别");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(11);
        cell.setCellValue("性别");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(12);
        cell.setCellValue("大类");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(13);
        cell.setCellValue("小类");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(14);
        cell.setCellValue("执行标准");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(15);
        cell.setCellValue("里料");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(16);
        cell.setCellValue("填充物");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(17);
        cell.setCellValue("撞料");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(18);
        cell.setCellValue("面料");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(19);
        cell.setCellValue("胆布");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(20);
        cell.setCellValue("备注");
        cell.setCellStyle(titleStyle);
        row.setHeight((short) 450);
        row = sheet.createRow(1);

        CellStyle titleStyle2 = wb.createCellStyle();
        titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        cell = row.createCell(0);
        cell.setCellValue("KEAY1008");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(1);
        cell.setCellValue("外套");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(2);
        cell.setCellValue("黑色;黄色;蓝色");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(3);
        cell.setCellValue("90;100;110;120");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(4);
        cell.setCellValue("120.00");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(5);
        cell.setCellValue("界内界外");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(6);
        cell.setCellValue("苏格马可");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(7);
        cell.setCellValue("2018");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(8);
        cell.setCellValue("春季");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(9);
        cell.setCellValue("中大童");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(10);
        cell.setCellValue("GB31701-2015.B类");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(11);
        cell.setCellValue("男");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(12);
        cell.setCellValue("服装");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(13);
        cell.setCellValue("开衫-毛衣/针织衫");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(14);
        cell.setCellValue("FZ/T 81014-2008");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(15);
        cell.setCellValue("100%棉");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(16);
        cell.setCellValue("100%聚酯纤维");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(17);
        cell.setCellValue("100%聚酯纤维");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(18);
        cell.setCellValue("90%棉9%粘纤1%涤纶");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(19);
        cell.setCellValue("防绒胆布");
        cell.setCellStyle(titleStyle2);
        cell = row.createCell(20);
        cell.setCellValue("备注信息");
        cell.setCellStyle(titleStyle2);
        for (int i = 0; i <= 20; i++) {
            if (i == 0 || i == 1 || i == 4 || i == 5 || i == 6 || i == 7 || i == 8 || i == 9 || i == 11 || i == 12) {
                sheet.setColumnWidth(i, 2100);
            } else {
                sheet.setColumnWidth(i, 4200);
            }

        }

        return wb;
    }

    @Override
    public List<Production> daoRuExcel(MultipartFile multipartFile, User user, String gongyingshang) {
        Set<String> set = new HashSet<String>();// 声明一个set类
        try {
            InputStream in = multipartFile.getInputStream();
            // 获取文件名称
            //  String fileName = multipartFile.getOriginalFilename();
            // fileName = fileName.substring(fileName.lastIndexOf(".") + 1);
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(in);
            Sheet sheet = xssfWorkbook.getSheetAt(0);
            String value = "";
            int cellcount = sheet.getRow(0).getLastCellNum();// 取得有效的列数
            int rowcount = sheet.getLastRowNum();// 取得有效的行数
            List<Production> productions = new ArrayList<Production>();
            for (int i = 1; i <= rowcount; i++) {
                Production production = new Production();
                for (int j = 0; j < cellcount; j++) {
                    Row row = sheet.getRow(i);
                    Cell cell = row.getCell(j);
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    value = cell.toString();
                    if (j != 14 && j != 15 && j != 16 && j != 17 && j != 19 && j != 20) {
                        if (value != null && !"".equals(value.trim())) {

                        } else {
                            throw new MyException("导入失败，表格第" + (i + 1) + "行,第" + (j + 1) + "列存在空的数据");
                        }
                    }

                    if (j == 0) {// 款号
                        production.setProduct(value);
                        int code = productionMapper.findByProduct(production);
                        if (code > 0) {
                            throw new MyException("导入失败，" + production.getProduct() + "已存在");
                        }
                    } else if (j == 1) {// 商品名
                        production.setProductName(value);
                    } else if (j == 2) {// 颜色
                        production.setColors(value);
                    } else if (j == 3) {// 尺码
                        production.setSizes(value);
                    } else if (j == 4) {// 铭牌价
                        production.setPrice(Double.parseDouble(value));
                    } else if (j == 5) {// 款式
                        production.setKuanshi(value);
                    } else if (j == 6) {// 品牌
                        production.setPinpai(value);
                    } else if (j == 7) {// 年份
                        production.setYear(value);
                    } else if (j == 8) {// 季节
                        production.setSeason(value);
                    } else if (j == 9) {// 款型
                        production.setStyle(value);
                    } else if (j == 10) {// 安全级别
                        production.setAnquanjibie(value);
                    } else if (j == 11) {// 性别
                        production.setSex(value);
                    } else if (j == 12) {// 大类
                        production.setDalei(value);
                    } else if (j == 13) {// 小类
                        production.setCategory(value);
                    } else if (j == 14) {// 执行标准
                        production.setZhixingbiaozhun(value);
                    } else if (j == 15) {// 里料
                        production.setLiliao(value);
                    } else if (j == 16) {// 填充物
                        production.setTianchongwu(value);
                    } else if (j == 17) {// 撞料
                        production.setZhuangliao(value);
                    } else if (j == 18) {// 面料
                        production.setMianliao(value);
                    } else if (j == 19) {// 胆布
                        production.setDanbu(value);
                    } else if (j == 20) {// 备注
                        production.setBeizhu(value);
                    }
                }
                production.setCreateBy(user.getId());
                production.setJgId(user.getJgid());
                production.setId(UUIDUtil.getUUID());
                production.setGongyingshang(gongyingshang);
                productions.add(production);
            }
            for (int i = 0; i < productions.size(); i++) {
                if (!set.add(productions.get(i).getProduct())) {// 如果set返回true，则没有重复
                    productions.remove(i);
                    --i;
                }
            }
            return productions;
        } catch (IOException e) {
            throw new RuntimeException("读取excel失败", e);
        }
    }


    @Override
    public int updateState(Production production) {
        //调用更新语句
        int code = productionMapper.updateState(production);
        if (code <= 0) {
            throw new MyException("修改状态失败，请检查信息重试!");
        }
        return code;
    }

    @Override
    @Transactional
    public int insertSome(List<Production> list) {
        //批量插入主表
        int code = productionMapper.insertSome(list);
        if (code <= 0) {
            throw new MyException("导入失败，请检出数据重试");
        }
        String color = "";
        String size = "";
        List<ProductionDetails> productionDetailses = new ArrayList<>();
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        for (int i = 0; i < list.size(); i++) {
            color = list.get(i).getColors();
            size = list.get(i).getSizes();
            color = color.replace(",", ";");
            color = color.replace("，", ";");
            color = color.replace("-", ";");
            color = color.replace(".", ";");
            color = color.replace("。", ";");
            color = color.replace("；", ";");
            size = size.replace(",", ";");
            size = size.replace("，", ";");
            size = size.replace("-", ";");
            size = size.replace(".", ";");
            size = size.replace("。", ";");
            size = size.replace("；", ";");
            String colors[] = color.split(";");
            String sizes[] = size.split(";");
            List<String> colorquchong = new ArrayList<String>();
            //颜色去重复
            for (int s = 0; s < colors.length; s++) {
                if (!colorquchong.contains(colors[s])) {
                    colorquchong.add(colors[s]);
                }
            }
            //尺码去重复
            List<String> sizequchong = new ArrayList<String>();
            for (int s = 0; s < sizes.length; s++) {
                if (!sizequchong.contains(sizes[s])) {
                    sizequchong.add(sizes[s]);
                }

            }
            int sum = 1000;
            for (int j = 0; j < colorquchong.size(); j++) {
                for (int k = 0; k < sizequchong.size(); k++) {
                    sum++;
                    ProductionDetails prd = new ProductionDetails();
                    prd.setColor(colorquchong.get(j));
                    prd.setSize(sizequchong.get(k));
                    prd.setProductCode(list.get(i).getProduct() + sum);
                    prd.setId(UUIDUtil.getUUID());
                    prd.setProductionId(list.get(i).getId());
                    productionDetailses.add(prd);
                }
            }
        }
        //插入子表
        int codes = productionDetailsMapper.insertSome(productionDetailses);
        if (codes <= 0) {
            throw new MyException("导入失败，请检出数据重试");
        }
        return code;
    }

    @Override
    @Transactional
    public int delProduction(Production production) {
        //查看是否存在裁剪单存在就不可删除
        int count = productionMapper.findTailor(production);
        if (count > 0) {
            throw new MyException("删除失败，该款号已在裁剪");
        }
        //删除状态是 1
        int flag = 1;
        production.setDeFlag(flag);
        int code = productionMapper.delProduction(production);
        if (code <= 0) {
            throw new MyException("删除失败，请刷新重试");
        }
        //删除子表信息
        code = productionDetailsMapper.delByProduction(production.getId());
        if (code <= 0) {
            throw new MyException("删除失败，请刷新重试");
        }
        return code;
    }

    @Override
    public Map<String, Object> findCombobox(String jgid) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("jgId", jgid);
        map.put("kuanshi", productionMapper.findKuanshi(maps));
        map.put("pinpai", productionMapper.findPinpai(maps));
        // map.put("jijie", productionMapper.findJijie(maps));
        map.put("kuanxing", productionMapper.findKuanxing(maps));
        map.put("anquanjibie", productionMapper.findAnquanjibie(maps));
        map.put("xiaolei", productionMapper.findXiaolei(maps));
        map.put("zhixingbiaozhun", productionMapper.findZhixingbiaozhun(maps));
        map.put("color", productionMapper.findColors(maps));
        map.put("size", productionMapper.findSizes(maps));
        map.put("gongyingshang", gongyingshangMapper.findAll(jgid));
        map.put("dalei", productionMapper.findDalei(maps));
        map.put("sex", productionMapper.findSex(maps));
        return map;
    }

    @Override
    @Transactional
    public int save(Production production) {
        String colors[] = production.getColors().split(",");
        String sizes[] = production.getSizes().split(",");
        List<ProductionDetails> ProductionDetailses = new ArrayList<ProductionDetails>();
        List<String> colorquchong = new ArrayList<String>();
        boolean bol = false;

        //颜色去重复
        for (int s = 0; s < colors.length; s++) {
            if (!colorquchong.contains(colors[s].trim())) {
                colorquchong.add(colors[s].trim());
            }
        }
        //尺码去重复
        List<String> sizequchong = new ArrayList<String>();
        for (int s = 0; s < sizes.length; s++) {
            if (!sizequchong.contains(sizes[s].trim())) {
                sizequchong.add(sizes[s].trim());
            }

        }
        for (int i = 0; i < colorquchong.size(); i++) {
            for (int j = 0; j < sizequchong.size(); j++) {
                ProductionDetails productionDetails2 = new ProductionDetails();
                productionDetails2.setId(UUIDUtil.getUUID());
                productionDetails2.setColor(colorquchong.get(i));
                productionDetails2.setSize(sizequchong.get(j));
                ProductionDetailses.add(productionDetails2);
            }
        }

        List<ProductionDetails> productionDetails = new ArrayList<ProductionDetails>();
        //修改
        if (production.getId() != null && production.getId() != "") {
            //根据id查询所有颜色和尺码
            productionDetails = productionDetailsMapper.findByProductionId(production.getId());
            //用来冗余的数据的id
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < productionDetails.size(); i++) {//数据库里的
                for (int j = 0; j < ProductionDetailses.size(); j++) {//页面上取得
                    //判断如果有颜色尺码相同就保存
                    if (productionDetails.get(i).getColor().equals(ProductionDetailses.get(j).getColor()) && productionDetails.get(i).getSize().equals(ProductionDetailses.get(j).getSize())) {
                        list.add(productionDetails.get(i).getId());
                        ProductionDetailses.remove(j);
                        break;
                    }
                }
            }
            //去除冗余的数据

            Map<String, Object> map = new HashMap<>();
            //插入空值sql不报错
            list.add("");
            map.put("productionId", production.getId());
            map.put("list", list);
            productionDetailsMapper.delSome(map);
            //更新主表
            int code = productionMapper.updateByPrimaryKeySelective(production);
            if (code <= 0) {
                throw new MyException("修改失败，请检查数据重试");
            }
            bol = true;
        } else {
            //新增production表
            production.setId(UUIDUtil.getUUID());
            int code = productionMapper.insertSelective(production);
            if (code <= 0) {
                throw new MyException("新增失败，请检查数据重试");
            }

        }
        int codes = 1;
        if (ProductionDetailses.size() > 0) {
            int sum = 1000;
            if (bol) {
                //获取第一个code值
                String code = productionDetails.get(0).getProductCode();
                if (StringUtils.isNumeric(code)) {
                    sum = Integer.parseInt(code.substring(code.length() - 4, code.length()));
                }
            }
            for (int i = 0; i < ProductionDetailses.size(); i++) {
                ProductionDetailses.get(i).setProductionId(production.getId());
                ProductionDetailses.get(i).setProductCode(production.getProduct() + (++sum));
            }
            codes = productionDetailsMapper.insertSome(ProductionDetailses);
            if (codes <= 0) {
                throw new MyException("新增失败，请检查数据重试");
            }
        }
        return codes;
    }

    @Override
    public int findByProduct(Production production) {
        return productionMapper.findByProduct(production);
    }

    @Override
    public Production findById(Production production) {

        Production pro = productionMapper.findById(production);
        //查看是否存在裁剪单存在就不可删除
        int count = productionMapper.findTailor(production);
        //查询入库单是否被引用
        int cous = rukuMapper.findCountByProductId(production.getId());
        //被引用了就setstate为1前台不给编辑
        if (count > 0 || cous > 0) {
            pro.setState(1);
        } else {
            pro.setState(0);
        }
        List<String> colors = new ArrayList<String>();
        List<String> sizes = new ArrayList<String>();
        for (int j = 0; j < pro.getProductionDetails().size(); j++) {
            //去重复
            if (!colors.contains(pro.getProductionDetails().get(j).getColor())) {
                colors.add(pro.getProductionDetails().get(j).getColor());
            }
            if (!sizes.contains(pro.getProductionDetails().get(j).getSize())) {
                sizes.add(pro.getProductionDetails().get(j).getSize());
            }
        }
        //将数组倒序排序
        Collections.reverse(sizes);
        pro.setColors(colors.toString());
        pro.setSizes(sizes.toString());
        pro.setProductionDetails(null);
        return pro;
    }

    @Override
    public List<Production> findProductCombobox(Production production) {
        return productionMapper.findProductCombobox(production);
    }

    @Override
    public List<Production> findProductByTailorId(Production production) {
        return productionMapper.findProductByTailorId(production);
    }

    /***
     * 获得所有款号不分页 销售用
     * @param u
     * @return
     */

    @Override
    public List<Production> findAllForJGService(User u) {
        Map<String, Object> mapCan = new HashMap<>();
        Set<Object> colorSet = new TreeSet<Object>();//颜色去重
        Set<Object> sizeSet = new TreeSet<Object>();//尺码去重
        mapCan.put("state", 0);
        mapCan.put("jgId", u.getJgid());
        List<Production> listPro = productionMapper.findAllForJG(mapCan);
        for (int i = 0; i < listPro.size(); i++) {
            for (int j = 0; j < listPro.get(i).getProductionDetails().size(); j++) {
                colorSet.add(listPro.get(i).getProductionDetails().get(j).getColor());
                sizeSet.add(listPro.get(i).getProductionDetails().get(j).getSize());

            }
            listPro.get(i).setColorArray(colorSet.toArray());
            listPro.get(i).setSizeArray(sizeSet.toArray());
            colorSet.clear();
            sizeSet.clear();

        }
        return listPro;
    }

    @Override
    public int saveDown(Production production) {
        //新增
        if (!StringUtils.isNotBlank(production.getId())) {
            int count = productionMapper.findByProduct(production);
            if (count > 0) {
                throw new MyException("已存在该商品，请确认数据后重新尝试");
            }
            if (!StringUtils.isNotBlank(production.getProduct())) {
                throw new MyException("新增商品失败，商品编号不能空");
            }
            if (!StringUtils.isNotBlank(production.getProductName())) {
                throw new MyException("新增商品失败，商品名不能空");
            }
            if (!StringUtils.isNotBlank(production.getSex())) {
                throw new MyException("新增商品失败，性别不能空");
            }
            if (!StringUtils.isNotBlank(production.getYear())) {
                throw new MyException("新增商品失败，年份不能空");
            }
            if (!StringUtils.isNotBlank(production.getSeason())) {
                throw new MyException("新增商品失败，季节不能空");
            }
            if (production.getPrice() <= 0) {
                throw new MyException("新增商品失败，铭牌价不能必须为大于零的数");
            }
            if (!StringUtils.isNotBlank(production.getColors())) {
                throw new MyException("新增商品失败，颜色不能为空");
            }
            if (!StringUtils.isNotBlank(production.getSizes())) {
                throw new MyException("新增商品失败，尺码不能为空");
            }
            //新增主表
            production.setId(UUIDUtil.getUUID());
            int code = productionMapper.insertSelective(production);
            if (code <= 0) {
                throw new MyException("新增商品失败，请检查数据重新尝试");
            }
            //新增子表
            ProductionDetails productionDetails = new ProductionDetails();
            productionDetails.setColor(production.getColors().trim());
            productionDetails.setSize(production.getSizes().trim());
            productionDetails.setId(UUIDUtil.getUUID());
            productionDetails.setProductCode(production.getProduct());
            productionDetails.setProductionId(production.getId());
            int cos = productionDetailsMapper.insert(productionDetails);
            if (cos <= 0) {
                throw new MyException("新增商品失败，请检查数据重新尝试");
            }
        } else {
            //修改  如果款号被引用就禁止修改款号其他可修改
            //更新主表
            int code = productionMapper.updateByPrimaryKeySelective(production);
            if (code <= 0) {
                throw new MyException("新增商品失败，请检查数据重新尝试");
            }
            ProductionDetails productionDetails = new ProductionDetails();
            productionDetails.setProductionId(production.getId());
            productionDetails.setProductCode(production.getProduct());
            productionDetails.setColor(production.getColors());
            productionDetails.setSize(production.getSizes());
            //更新子表
            productionDetailsMapper.updateByProductId(productionDetails);
        }
        return 1;
    }

    @Override
    public List<Production> findProductComboboxZongbu() {
        return productionMapper.findProductComboboxZongbu();
    }

    @Override
    public List<Production> findCipinForProduct() {
        return productionMapper.findCipinForProduct();
    }

    @Override
    public List<ProductionDetails> findSaleProductCode(Production production) {
        Map<String, Object> map = new HashMap<>();
        map.put("jgId", production.getJgId());
        if (StringUtils.isNotBlank(production.getProduct())) {
            map.put("productCode", production.getProduct());
        }
        return productionMapper.findSaleProductCode(map);
    }

    @Override
    public Sale findSaleByProductionDetailsId(Sale sale) {
        return productionMapper.findSaleByProductionDetailsId(sale);
    }

    /***
     * 出库退货：出库商品
     * @param chukuId
     * @return
     */

    @Override
    public List<Production> findAllProductForChuKuId(String chukuId) {
        Map<String, Object> mapCan = new HashMap<>();
        mapCan.put("chukuId", chukuId);
        List<Production> proList = productionMapper.findAllProductForChuKuId(mapCan);//本出库单商品
        Set<Object> colorSet = new TreeSet<Object>();//颜色去重
        Set<Object> sizeSet = new TreeSet<Object>();//尺码去重


        if (proList != null && proList.size() != 0) {//判断集合是否为空
            for (int i = 0; i < proList.size(); i++) {
                for (int j = 0; j < proList.get(i).getProductionDetails().size(); j++) {
                    colorSet.add(proList.get(i).getProductionDetails().get(j).getColor());
                    sizeSet.add(proList.get(i).getProductionDetails().get(j).getSize());
                }
                proList.get(i).setColorArray(colorSet.toArray());
                proList.get(i).setSizeArray(sizeSet.toArray());
                colorSet.clear();
                sizeSet.clear();
            }
        } else {
            throw new MyException("暂无数据!");
        }
        return proList;
    }

    @Override
    public List<Combobox> findSeasonCombobox(String jgid) {
        Map<String, Object> map = new HashMap<>();
        map.put("jgId", jgid);
        return productionMapper.findJijie(map);
    }

    /**
     * 退货根据客户获得此客户所有商品
     *
     * @return
     */
    @Override
    public List<Production> selectChuKuProductForKeHuIdService(String kehuId) {
        Map<String, Object> mapCan = new HashMap<>();
        mapCan.put("kehuId", kehuId);
        List<Production> listPro = productionMapper.selectChuKuProductForKeHuId(mapCan);
        Set<Object> colorSet = new TreeSet<Object>();//颜色去重
        Set<Object> sizeSet = new TreeSet<Object>();//尺码去重
        if (listPro != null && listPro.size() != 0)
            for (int i = 0; i < listPro.size(); i++) {
                for (int j = 0; j < listPro.get(i).getProductionDetails().size(); j++) {
                    colorSet.add(listPro.get(i).getProductionDetails().get(j).getColor());
                    sizeSet.add(listPro.get(i).getProductionDetails().get(j).getSize());
                }
                listPro.get(i).setColorArray(colorSet.toArray());
                listPro.get(i).setSizeArray(sizeSet.toArray());
                colorSet.clear();
                sizeSet.clear();
            }

        return listPro;
    }

    /***
     * 获得商品属性
     * @param u
     * @return
     */
    @Override
    public Map<String, Object> selectAllAttrService(User u) {
        Map<String, Object> mapCan = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        mapCan.put("jgId", u.getJgid());
        map.put("pinpaix", productionMapper.selectAllAttrPinPai(mapCan));
        map.put("yearx", productionMapper.selectAllAttrYear(mapCan));
        map.put("seasonx", productionMapper.selectAllAttrSeason(mapCan));
        return map;
    }

}
